home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 20 / Cream of the Crop 20 (Terry Blount) (1996).iso / program / pcl4w13.zip / SIMPLE.C < prev    next >
Text File  |  1996-07-14  |  10KB  |  320 lines

  1. /*
  2. **                    --- simple.c ---
  3. **
  4. **  EXAMPLE CODE: Very simple terminal emulator.
  5. **
  6. **  This example program (not the PCL4W library) is donated to
  7. **  the Public Domain by MarshallSoft Computing, Inc. It is
  8. **  provided as an example of the use of the PCL4W.
  9. **
  10. */
  11.  
  12. #define USECOMM
  13.  
  14. #include "windows.h"
  15. #include "stdio.h"
  16. #include "pcl4w.h"
  17. #include "sioerror.h"
  18. #include "ascii.h"
  19. #include "simple.h"
  20. #include "message.h"
  21. #include "line.h"
  22. #include "paint.h"
  23. #include "about.h"
  24.  
  25. /* public globals */
  26.  
  27. HWND hMainWnd;            /* main window handle */
  28. int OnLineFlag = FALSE;   /* TRUE: online */
  29. int FatalFlag = FALSE;    /* TRUE: fatal error */
  30.  
  31. /* private globals */
  32.  
  33. static HMENU hMenu;
  34. static HANDLE hInstance;
  35. static int ThePort = COM1;
  36. static int TheBaud = Baud19200;
  37. static int WinWidth = 8 * NCOLS;
  38. static int WinHeight = 12 * NROWS + 48;
  39. static long BaudRateList[10]
  40.        = {300L,600L,1200L,2400L,4800L,9600L,19200L,38400L,57600L,115200L};
  41. static char Temp[1024];
  42.  
  43. /* miscellaneous functions */
  44.  
  45. void ErrorCheck(int);
  46.  
  47. int PASCAL WinMain(HANDLE hInst,HANDLE hPrevInstance,
  48.                    LPSTR lpCmdLine,int nCmdShow)
  49. {WNDCLASS  wc;
  50.  MSG msg;
  51.  BOOL Result;
  52.  if(!hPrevInstance)
  53.    {/* register main window class */
  54.     wc.style = CS_HREDRAW | CS_VREDRAW;
  55.     wc.lpfnWndProc = MainWndProc;
  56.     wc.cbClsExtra = 0;
  57.     wc.cbWndExtra = 0;
  58.     wc.hInstance = hInst;
  59.     wc.hIcon = LoadIcon(hInst, "SimpleIcon");
  60.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  61.     wc.hbrBackground = GetStockObject(WHITE_BRUSH);
  62.     wc.lpszMenuName =  "SimpleMenu";
  63.     wc.lpszClassName = "SimpleWClass";
  64.     Result = RegisterClass(&wc);
  65.     if(!Result) return FALSE;
  66.    }
  67.  
  68.  /* create main window */
  69.  hInstance = hInst;
  70.  hMainWnd = CreateWindow(
  71.         "SimpleWClass",   "SIMPLE",       WS_OVERLAPPEDWINDOW,
  72.         CW_USEDEFAULT,    CW_USEDEFAULT,
  73.         WinWidth,         WinHeight,
  74.         NULL,             NULL,
  75.         hInstance,        NULL);
  76.  ShowWindow(hMainWnd, nCmdShow);
  77.  UpdateWindow(hMainWnd);
  78.  hMenu = GetMenu(hMainWnd);
  79.  
  80.  /* window control loop */
  81.  
  82.  while(GetMessage(&msg,NULL,NULL,NULL))
  83.    {
  84.     TranslateMessage(&msg);
  85.     DispatchMessage(&msg);
  86.    }
  87.  return (msg.wParam);
  88. } /* end WinMain */
  89.  
  90. long FAR PASCAL MainWndProc(HWND hWindow,UINT message,WPARAM wParam,LPARAM lParam)
  91. {
  92.  UINT idTimer;
  93.  HDC hDC;
  94.  PAINTSTRUCT ps;
  95.  int i;
  96.  int TheChar;
  97.  int Count;
  98.  static FARPROC lpProcAbout;
  99.  
  100.  
  101.  hMainWnd = hWindow;
  102.  switch (message)
  103.     {
  104.      case WM_COMMAND:
  105.          switch(wParam)
  106.            {case MSG_ABOUT:
  107.               DialogBox(hInstance,"AboutBox",hMainWnd,lpProcAbout);
  108.               break;
  109.  
  110.             case MSG_DEBUG:
  111.               break;
  112.  
  113.             case MSG_ONLINE:
  114.               if(OnLineFlag) break;
  115.               else
  116.                 {/* try to go on-line */
  117.                  if(GoOnLine(ThePort,TheBaud,Size1024,Size1024))
  118.                    {/* we're online */
  119.                     OnLineFlag = TRUE;
  120.                     sprintf(Temp,"SIMPLE: COM%d online at %ld baud",
  121.                        1+ThePort,BaudRateList[TheBaud]);
  122.                     SetWindowText(hMainWnd,Temp);
  123.                     CheckMenuItem(hMenu,MSG_OFFLINE,MF_BYCOMMAND | MF_UNCHECKED);
  124.                     CheckMenuItem(hMenu,MSG_ONLINE,MF_BYCOMMAND | MF_CHECKED);
  125.                     for(i=MSG_COM1;i<=MSG_COM4;i++)
  126.                       EnableMenuItem(hMenu,i,MF_BYCOMMAND|MF_GRAYED);
  127.                     for(i=MSG_300;i<=MSG_115200;i++)
  128.                       EnableMenuItem(hMenu,i,MF_BYCOMMAND|MF_GRAYED);
  129.                    }
  130.                 }
  131.               break;
  132.  
  133.             case MSG_OFFLINE:
  134.               if(!OnLineFlag) break;
  135.               GoOffLine(ThePort);
  136.               OnLineFlag = FALSE;
  137.               SetWindowText(hMainWnd,"SIMPLE: Offline");
  138.               CheckMenuItem(hMenu,MSG_ONLINE,MF_BYCOMMAND | MF_UNCHECKED);
  139.               CheckMenuItem(hMenu,MSG_OFFLINE,MF_BYCOMMAND | MF_CHECKED);
  140.               for(i=MSG_COM1;i<=MSG_COM4;i++)
  141.                 EnableMenuItem(hMenu,i,MF_BYCOMMAND|MF_ENABLED);
  142.               for(i=MSG_300;i<=MSG_115200;i++)
  143.                 EnableMenuItem(hMenu,i,MF_BYCOMMAND|MF_ENABLED);
  144.               break;
  145.  
  146.             case MSG_EXIT:
  147.               DestroyWindow(hMainWnd);
  148.               break;
  149.  
  150.             case MSG_COM1:
  151.               CheckMenuItem(hMenu,MSG_COM1+ThePort,MF_BYCOMMAND | MF_UNCHECKED);
  152.               ThePort = COM1;
  153.               CheckMenuItem(hMenu,MSG_COM1,MF_BYCOMMAND | MF_CHECKED);
  154.               break;
  155.  
  156.             case MSG_COM2:
  157.               CheckMenuItem(hMenu,MSG_COM1+ThePort,MF_BYCOMMAND | MF_UNCHECKED);
  158.               ThePort = COM2;
  159.               CheckMenuItem(hMenu,MSG_COM2,MF_BYCOMMAND | MF_CHECKED);
  160.               break;
  161.  
  162.             case MSG_COM3:
  163.               CheckMenuItem(hMenu,MSG_COM1+ThePort,MF_BYCOMMAND | MF_UNCHECKED);
  164.               ThePort = COM3;
  165.               CheckMenuItem(hMenu,MSG_COM3,MF_BYCOMMAND | MF_CHECKED);
  166.               break;
  167.  
  168.             case MSG_COM4:
  169.               CheckMenuItem(hMenu,MSG_COM1+ThePort,MF_BYCOMMAND | MF_UNCHECKED);
  170.               ThePort = COM4;
  171.               CheckMenuItem(hMenu,MSG_COM4,MF_BYCOMMAND | MF_CHECKED);
  172.               break;
  173.  
  174.             case MSG_300:
  175.               CheckMenuItem(hMenu,MSG_300+TheBaud,MF_BYCOMMAND | MF_UNCHECKED);
  176.               TheBaud = Baud300;
  177.               CheckMenuItem(hMenu,MSG_300,MF_BYCOMMAND | MF_CHECKED);
  178.               break;
  179.  
  180.             case MSG_1200:
  181.               CheckMenuItem(hMenu,MSG_300+TheBaud,MF_BYCOMMAND | MF_UNCHECKED);
  182.               TheBaud = Baud1200;
  183.               CheckMenuItem(hMenu,MSG_1200,MF_BYCOMMAND | MF_CHECKED);
  184.               break;
  185.  
  186.             case MSG_2400:
  187.               CheckMenuItem(hMenu,MSG_300+TheBaud,MF_BYCOMMAND | MF_UNCHECKED);
  188.               TheBaud = Baud2400;
  189.               CheckMenuItem(hMenu,MSG_2400,MF_BYCOMMAND | MF_CHECKED);
  190.               break;
  191.  
  192.             case MSG_4800:
  193.               CheckMenuItem(hMenu,MSG_300+TheBaud,MF_BYCOMMAND | MF_UNCHECKED);
  194.               TheBaud = Baud4800;
  195.               CheckMenuItem(hMenu,MSG_4800,MF_BYCOMMAND | MF_CHECKED);
  196.               break;
  197.  
  198.             case MSG_9600:
  199.               CheckMenuItem(hMenu,MSG_300+TheBaud,MF_BYCOMMAND | MF_UNCHECKED);
  200.               TheBaud = Baud9600;
  201.               CheckMenuItem(hMenu,MSG_9600,MF_BYCOMMAND | MF_CHECKED);
  202.               break;
  203.  
  204.             case MSG_19200:
  205.               CheckMenuItem(hMenu,MSG_300+TheBaud,MF_BYCOMMAND | MF_UNCHECKED);
  206.               TheBaud = Baud19200;
  207.               CheckMenuItem(hMenu,MSG_19200,MF_BYCOMMAND | MF_CHECKED);
  208.               break;
  209.  
  210.             case MSG_38400:
  211.               CheckMenuItem(hMenu,MSG_300+TheBaud,MF_BYCOMMAND | MF_UNCHECKED);
  212.               TheBaud = Baud38400;
  213.               CheckMenuItem(hMenu,MSG_38400,MF_BYCOMMAND | MF_CHECKED);
  214.               break;
  215.  
  216.             case MSG_57600:
  217.               CheckMenuItem(hMenu,MSG_300+TheBaud,MF_BYCOMMAND | MF_UNCHECKED);
  218.               TheBaud = Baud57600;
  219.               CheckMenuItem(hMenu,MSG_57600,MF_BYCOMMAND | MF_CHECKED);
  220.               break;
  221.  
  222.             case MSG_115200:
  223.               CheckMenuItem(hMenu,MSG_300+TheBaud,MF_BYCOMMAND | MF_UNCHECKED);
  224.               TheBaud = Baud115200;
  225.               CheckMenuItem(hMenu,MSG_115200,MF_BYCOMMAND | MF_CHECKED);
  226.               break;
  227.  
  228.             default:
  229.               return (DefWindowProc(hMainWnd, message, wParam, lParam));
  230.            }
  231.          break;
  232.  
  233.     case WM_CREATE:
  234.  
  235.       /* check "OFFLINE" menu item */
  236.       hMenu = GetMenu(hMainWnd);
  237.       CheckMenuItem(hMenu,MSG_OFFLINE,MF_BYCOMMAND | MF_CHECKED);
  238.       /* check default COM port */
  239.       CheckMenuItem(hMenu,MSG_COM1+ThePort,MF_BYCOMMAND | MF_CHECKED);
  240.       /* check default baud rate */
  241.       CheckMenuItem(hMenu,MSG_300+TheBaud,MF_BYCOMMAND | MF_CHECKED);
  242.       /* create AboutDlgProc() thunk */
  243.       lpProcAbout = MakeProcInstance(AboutDlgProc, hInstance);
  244.       /* initialize paint module */
  245.       InitPaint(